package com.sgx.learning.luban.data_structure;

import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * 广度搜索
 */
public class BFS {

    private int n;      //表示行
    private int m;      //表示列

    private int dx;     //表示当前位置的x坐标
    private int dy;     //表示当前位置的y坐标

    private int data[][];
    private boolean mark[][];

    public BFS(int n, int m, int dx, int dy, int[][] data, boolean[][] mark) {
        this.n = n;
        this.m = m;
        this.dx = dx;
        this.dy = dy;
        this.data = data;
        this.mark = mark;
    }

    public void bfs(int startX,int startY) {
        mark[startX][startY] = true;
        Queue<Point> queue = new ArrayBlockingQueue<>(n * m);
        Point start = new Point();
        start.x = startX;
        start.y = startY;
        queue.add(start);
        //方向向量
        int next[][] = {{0,1},{1,0},{0,-1},{-1,0}};
        while(!queue.isEmpty()) {
            //判断前后左右
            Point curPoint = queue.poll();
            for (int i = 0;i < 4; i++) {
                int nextX = curPoint.x + next[i][0];
                int nextY = curPoint.y + next[i][1];
                if(nextX < 1 || nextX > n || nextY < 1 || nextY > m) {

                } else {
                    if(!mark[nextX][nextY] && data[nextX][nextY] == 0) {
                        if(nextX == dx && nextY == dy) {
                            System.out.println("true");
                            return;
                        }
                        Point nextPoint = new Point();
                        nextPoint.x = nextX;
                        nextPoint.y = nextY;
                        queue.add(nextPoint);
                        mark[nextX][nextY] = true;
                    }
                }
            }
        }
        System.out.println("false");
    }

    /**
     *     0 0 1 0
     *     0 0 0 0
     *     0 0 1 0
     *     0 1 0 0
     *     0 0 0 1
     */
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int m = cin.nextInt();

        int data[][] = new int[n+1][m+1];
        for (int i = 1; i <= n ; i++) {
            for (int j = 1; j <= m ; j++) {
                data[i][j] = cin.nextInt();
            }
        }
        int dx = cin.nextInt();
        int dy = cin.nextInt();
        boolean mark[][] = new boolean[n+1][m+1];
        BFS bfs = new BFS(n,m,dx,dy,data,mark);
        int startX = cin.nextInt();
        int startY = cin.nextInt();
        bfs.bfs(startX,startY);
    }
}

class Point{
    int x;
    int y;
}
